# -*- coding:utf-8 -*-
import zipfile

import web
import json
import re
from lafeier.AutoStruct import *
from config import *
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
urls = (
    "/struct", "struct",
  "/structdata", "structdata",
    "/gettpl", "gettpl"
)
struct_app = web.application(urls, globals())

#获取模板列表
class gettpl:
    def GET(self):
        web.header('Access-Control-Allow-Origin', '*')
        web.header("Content-Type", "Application/json; charset=utf-8")
        r=getrdc()
        tpldata = r.lrange('tpl',0,-1)
        b = {}
        a = []
        for i in tpldata:
            o = eval(i)
            a.append(o)
        b['data'] = a
        return json.dumps(b,ensure_ascii=False)


class struct:
    def GET(self):
        web.header("Content-Type", "text/html; charset=utf-8")
        return """<html><head></head><body>
    <form method="POST" enctype="multipart/form-data" action="http://zrb.gotohard.cn/struct">
    <input type="file" name="myfile" />
    <br/>
    <input type="text" name="select"/>
    <input type="submit" />
    </form>
    </body></html>"""
    def POST(self):
        web.header('Access-Control-Allow-Origin', '*')
        web.header("Content-Type", "Application/json; charset=utf-8")
        reload(sys)
        sys.setdefaultencoding('utf-8')
        # web.header("Content-Type", "text/html; charset=utf-8")
        x = web.input(myfile={})
        d = web.input()
        # j=web.data()
        # da=json.loads(d)
        filedir = path+'/jiegouhua/'  # change this to the directory you want to store the file in.
        # return filedir
        if 'myfile' in x:  # to check if the file-object is created
            filepath = x['myfile'].filename.replace('\\', '/')  # replaces the windows-style slashes with linux ones.
            filename = filepath.split('/')[-1]  # splits the and chooses the last part (the filename with extension)
            filename = filename.decode('utf-8')
            ishtml = re.search('html',filename)
            if ishtml:
                #单个文件结构化
                filename = filename.decode('utf-8')
                htmlpath = filedir +'structhtml/'+ filename.encode('utf-8')
                fout = open(htmlpath, 'wb')  # creates the file where the uploaded file should be stored
                fout.write(x['myfile'].file.read())  # writes the uploaded file to the newly created file.
                fout.close()
                tplname = x['select']
                # 模板名字，用来存数据库
                tplpath = path+'/jiegouhua/tpl/' + tplname

                try:
                    data = struct(htmlFilePath=htmlpath, tplFilePath=tplpath,encoding = 'utf-8')
                except (UnicodeDecodeError, UnicodeEncodeError):
                    try:
                        data = struct(htmlFilePath=htmlpath, tplFilePath=tplpath,encoding='gb18030')

                    except (UnicodeDecodeError, UnicodeEncodeError):
                        try:
                            data = struct(htmlFilePath=htmlpath, tplFilePath=tplpath,encoding='gbk')
                        except (UnicodeDecodeError, UnicodeEncodeError):
                            data = struct(htmlFilePath=htmlpath, tplFilePath=tplpath,encoding='gb2312')
                with open('./bugg.txt','a') as f:
                    f.write(str(data))
                r=getrdc()
                r.lpush(tplname, json.dumps(data['data']))
                pass
            else:
                #网页压缩包结构化
                filename = filename.decode('utf-8')
                zippath = filedir+'htmlzip/'+filename.encode('utf-8')
                # return filepath, filename, chardet.detect(filepath),filepath.decode('utf-8'),chardet.detect(filename),filename.decode('utf-8')
                # return zippath
                fout = open(zippath, 'wb')  # creates the file where the uploaded file should be stored
                fout.write(x['myfile'].file.read())  # writes the uploaded file to the newly created file.
                fout.close()  # closes the file, upload complete.
                # return "filename: %s\n " % (x['myfile'].filename)
                tplname=x['select']
                #模板名字，用来存数据库

                #构造启解压后的文件目录
                zip_un_html = filename.split('.')
                savePath = filedir+'htmlzip/' + zip_un_html[0]
                savePath = savePath.encode('utf-8')
                tplpath=path+'/jiegouhua/tpl/'+tplname
                dictor = {}
                # return zippath.decode('utf-8'), savePath.decode('utf-8'), tplpath.decode('utf-8'), rtplname[0],
                try:

                    alldata = structure(zipFileName=zippath, savePath=savePath, tpl=tplpath,
                                     tplname=tplname, encoding='utf-8')
                    dictor['data1']=alldata
                except (UnicodeDecodeError, UnicodeEncodeError):
                    try:

                        alldata = structure(zipFileName=zippath, savePath=savePath, tpl=tplpath,
                                         tplname=tplname, encoding='GB18030')

                        dictor['data2'] = alldata
                    except (UnicodeDecodeError, UnicodeEncodeError):
                        try:
                            alldata = structure(zipFileName=zippath, savePath=savePath, tpl=tplpath,
                                             tplname=tplname, encoding='GB2312')

                            dictor['data3'] = alldata
                        except (UnicodeDecodeError, UnicodeEncodeError):
                            alldata = structure(zipFileName=zippath, savePath=savePath, tpl=tplpath,
                                             tplname=tplname, encoding='GBK')
                            dictor['data4'] = alldata
                dirnum = lookThrough(savePath)
                dictor['dirnum'] = dirnum
                return dictor
        else:
            return 111


class structdata:
    def GET(self):
        web.header('Access-Control-Allow-Origin', '*')
        web.header("Content-Type", "Application/json; charset=utf-8")
        r=getrdc()
        i = web.input()
        tplname = i['tplname']
        page = i['page']
        num = 2
        start = int(num)*int(page)
        end = int(num)*(int(page)+1)
        data = r.lrange(tplname, start=start, end=end)
        datalenth = r.llen(tplname)
        if datalenth:
            dic = {}
            li = []
            for i in data:
                da = eval(i)
                li.append(da)
            dic['data']=li
            if dic['data']:
                return json.dumps(dic,ensure_ascii=False)
            else:
                if num*page>datalenth:
                    return 22
        else:
            return 11
        # 结构化
            #

# 解压函数
def zipExtract(filename, savePath,encoding ='utf-8'):
    file = zipfile.ZipFile(filename, "r")
    for name in file.namelist():
        utf8name = name
        dirname = savePath + '/' + os.path.dirname(utf8name)
        try:
            if not os.path.exists(dirname) and dirname != "":
                os.makedirs(dirname)
        except (UnicodeDecodeError, UnicodeEncodeError):
            try:
                dirname = dirname.decode('GBK').encode('utf-8')
                if not os.path.exists(dirname) and dirname != "":
                    os.makedirs(dirname)
            except (UnicodeDecodeError, UnicodeEncodeError):
                try:
                    dirname = dirname.decode('GB18030').encode('utf-8')
                    if not os.path.exists(dirname) and dirname != "":
                        os.makedirs(dirname)
                except (UnicodeDecodeError, UnicodeEncodeError):
                    dirname = dirname.decode('GBK2312').encode('utf-8')
                    if not os.path.exists(dirname) and dirname != "":
                        os.makedirs(dirname)
        data = file.read(name)
        dirname_encoding = getStrCoding(utf8name)
        utf8name = utf8name.decode(dirname_encoding).encode('utf-8')
        # utf8name = re.sub(r'\\\\',r'\\',utf8name)
        # b = utf8name.decode("string_escape")
        savedir = savePath + '/' + utf8name
        with open('/var/www/zrbspider/test.txt','w')as f:
            f.write(savedir)
        if not os.path.exists(os.path.dirname(os.path.abspath(savedir))):
            os.makedirs(os.path.dirname(os.path.abspath(savedir)))
        if not os.path.exists(savedir):
            fo = open(savedir, "w")
            fo.write(data)
            fo.close
        file.close()
        pass
# 遍历目录函数（去除目录，只保留文件）
def lookThrough(rootDir, dirs=set()):
    for lists in os.listdir(rootDir):
        path = os.path.join(rootDir, lists)
        if os.path.isdir(path):
            lookThrough(path)
        else:
            dirs.add(path)
    return list(dirs)

#检测编码
def getStrCoding(str):
    def getCoding(str):
        try:
            coding = codingList.pop()
            str.decode(coding)
            return coding
        except:
            return getCoding(str)
    codingList = ['gb18030', 'gbk','gb2312', 'utf-8']
    return getCoding(str)
def structure(zipFileName, savePath, tpl, tplname, encoding='utf-8'):
    # 解压
    zipExtract(zipFileName, savePath, encoding=encoding)
    # 遍历文件
    alldata = []
    for url in lookThrough(savePath):
        try:
            data = struct(htmlFilePath=url, tplFilePath=tpl,encoding =encoding)
        except :
            with open(path+'/buuug.txt','a') as f:
                f.write(str(data)+'123')
            continue
        r=getrdc()
        r.lpush(tplname, json.dumps(data['data']))
        alldata.append(data['data'])
    return alldata
    pass


